package com.yuanlp.shoppingmall.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.yuanlp.shoppingmall.R;
import com.yuanlp.shoppingmall.base.BaseFragement;
import com.yuanlp.shoppingmall.community.CommunityFragment;
import com.yuanlp.shoppingmall.home.fragment.HomeFragment;
import com.yuanlp.shoppingmall.shoppingcart.fragment.ShoppingFragment;
import com.yuanlp.shoppingmall.type.TypeFragement;
import com.yuanlp.shoppingmall.user.UserFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends FragmentActivity {
    private static final String TAG = "MainActivity";

    @BindView(R.id.framelayout)
    FrameLayout mFramelayout;
    @BindView(R.id.rb_home)
    RadioButton mRbHome;
    @BindView(R.id.rb_type)
    RadioButton mRbType;
    @BindView(R.id.rb_community)
    RadioButton mRbCommunity;
    @BindView(R.id.rb_cart)
    RadioButton mRbCart;
    @BindView(R.id.rb_user)
    RadioButton mRbUser;
    @BindView(R.id.rg_main)
    RadioGroup mRgMain;

    public static List<BaseFragement> baseList;
    public static  int position;  //当前选中的那个页面的位置
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定butterknife与activity，实例化控件
        ButterKnife.bind(this);

        baseList=new ArrayList<BaseFragement>();
        baseList.add(new HomeFragment(this));
        baseList.add(new ShoppingFragment(this));
        baseList.add(new TypeFragement());
        baseList.add(new CommunityFragment());
        baseList.add(new UserFragment());

        mRgMain.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
        mRgMain.check(R.id.rb_home);

    }

    class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId){
                case R.id.rb_home:

                    position=0;
                    System.out.println("onCheckedChanged:--------111----------------------------------------- ");
                    Log.d(TAG, "onCheckedChanged:--------111----------------------------------------- ");
                    break;
                case R.id.rb_cart:
                    position=1;
                    break;
                case R.id.rb_type:
                    position=2;
                    break;
                case R.id.rb_community:
                    position=3;
                    break;
                case R.id.rb_user:
                    position=4;
                    break;
                default:
                    position=0;
                    Log.d(TAG, "onCheckedChanged:------------------------------------------------- ");
                    break;

            }
            setFragement();
        }

        private void setFragement() {
            System.out.println("setFragement: --------------------------------------------");
            Log.d(TAG, "setFragement: --------------------------------------------");
            FragmentManager fragmentManager=getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            BaseFragement baseFragement=baseList.get(position);
            transaction.replace(R.id.framelayout,baseFragement);
            transaction.commit();
        }
    }

}
